Component({
  properties: {
    domain: {
      type: String,
      value: ''
    },
    payParam: {
      type: Object,
      value: {}
    }
  },
  lifetimes: {
    attached() {
      this.init()
    }
  },
  observers: {
    payParam() {
      this.init()
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    hasPay: false,
    payResult: false,
    timeStamp: '',
    nonceStr: '',
    package: '',
    signType: '',
    paySign: '',
    orderCode: '',
    notice: '支付中...',
    desc: '',
    needDispatch: false,
    orderPath: '',
    doMain: ''
  },
  methods: {
    init() {
      if (this.properties.payParam && this.properties.payParam.timeStamp) {
        this.setData({
          timeStamp: this.properties.payParam.timeStamp,
          nonceStr: this.properties.payParam.nonceStr,
          package: decodeURIComponent(this.properties.payParam.package),
          signType: this.properties.payParam.signType,
          paySign: this.properties.payParam.paySign,
          orderCode: this.properties.payParam.orderCode,
          needDispatch: this.properties.payParam.needDispatch,
          orderPath: this.properties.payParam.orderPath,
          doMain: decodeURIComponent(this.properties.payParam.doMain)
        })
        this.pay()
      }
    },
    pay() {
      if (this.data.timeStamp &&
          this.data.nonceStr &&
          this.data.package &&
          this.data.signType &&
          this.data.paySign) {
        const _this = this
        wx.requestPayment({
          timeStamp: _this.data.timeStamp,
          nonceStr: _this.data.nonceStr,
          package: _this.data.package,
          signType: _this.data.signType,
          paySign: _this.data.paySign,
          success() {
            _this.setData({
              hasPay: true,
              payResult: true,
              iconType: 'success',
              notice: '支付完成',
              desc: ''
            })
          },
          fail(res) {
            if (res.errMsg === 'requestPayment:fail cancel') {
              // 用户取消支付
              _this.setData({
                hasPay: true,
                payResult: false,
                iconType: 'warn',
                notice: '支付已取消',
                desc: '支付未完成，请尽快支付以免订单超时被关闭。'
              })
            } else {
              _this.setData({
                hasPay: true,
                payResult: false,
                iconType: 'warn',
                notice: '支付遇到了问题',
                desc: '支付遇到了未知问题，请稍后重试。'
              })
            }
          }
        })
      }
    },
    goOrder() {
      this.setUrl((this.data.doMain || this.properties.domain) + this.data.orderPath)
      /* this.setUrl(this.properties.domain +
       (!this.data.needDispatch || this.data.needDispatch ===
         'false' ? '/ownspace/1' : '/ownspace/9')) */
    },
    goIndex() {
      this.setUrl(this.data.doMain || this.properties.domain)
    },
    setUrl(url) {
      console.log('aaaaaaaa', url)
      const _this = this
      wx.login({
        success(res) {
          if (res.code) {
            let connect = '?'
            if (url.indexOf('?') > 0) {
              connect = '&'
            }
            _this.setData({
              url: url + connect + 'miniCode=' + res.code
            })
          }
        }
      })
    }
  }
})
